package com.winit.emListener.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;
import com.winit.clQueue.model.ClQueue;
import com.winit.clQueue.service.ClQueueService;
import com.winit.common.openApi.JsonObjectUtil;
import com.winit.config.model.Config;
import com.winit.config.service.IConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import java.util.*;


/**
 * Created by wanglin on 2017/7/19.
 */
public class EmJobService implements MessageListener {

    private static Logger logger = LoggerFactory.getLogger(EmJobService.class);

    private static Gson gson=new Gson();

    @Autowired
    ClQueueService clQueueService;

    @Autowired
    IConfigService configService;
    @Override
    public void onMessage(Message message){
//        try{

            String messageType = message.getMessageProperties().getHeaders().get("messageType").toString();
            String appid = message.getMessageProperties().getHeaders().get("fromAppId").toString();
            logger.info("name:"+messageType+",appid:"+appid);
            ClQueue clQueue = clQueueService.findNameAndAppId(messageType, appid, "EVEN");
            if(null == clQueue){
                String msg=new String(message.getBody());
                JsonObject body = gson.fromJson(msg, JsonObject.class);
                try{
                    String[] str=appid.split("\\.");
                    String zone=str[2]+"."+str[3];
                    JsonObject payload = body.get("payload").getAsJsonObject();
                    System.out.println(payload);
                    JSONObject json = JSONObject.parseObject(payload.toString());
                    JsonObject headers= body.get("headers").getAsJsonObject();
                    json= JsonObjectUtil.parseJsonString(json);
                    Map map=new HashMap<>();
                    map.put("param",json.toJSONString());
                    Config config=new Config();
                    config.setKVMap(map);
                    configService.saveConfig(config);
                    ClQueue cq=new ClQueue();
                    cq.setAppId(appid);
                    cq.setName(messageType);
                    cq.setParamConfigId(config.getId());
                    cq.setZoneCode(zone);
                    cq.setType("EVEN");
                    cq.setAppType(headers.get("module").getAsString());
                    clQueueService.saveClQueue(cq);
                }catch (Exception e){
                    logger.error("保存时异常:"+e.getMessage());
                }
            }
//        }catch (Exception e){
//            logger.error("消息异常:"+e.getMessage());
//        }
    }






}







